package com.jadcon.harveycedars.model.dormitory;

import org.springframework.validation.Errors;
import org.springframework.validation.Validator;

public class DormitoryValidator implements Validator {
    
    /**
    * This Validator validates just Dormitory instances
    */
    public boolean supports(Class clazz) {
        return Dormitory.class.equals(clazz);
    }
    
    /**
     * Does the actual validation.  Validation errors
     * are set on the Errors class that Spring provides.
     * Later the errors will be displayed to the user.
     */
    public void validate(Object object, Errors e) {
        Dormitory dormitory = (Dormitory) object;
        String name = dormitory.getName();
        if (name == null || "".equals(name)) {
        	// if the name is null or empty,
        	// create an error message.
        	// The first parameter is the name
        	// of the field (which is this case is "name")
        	// and the 2nd parameter is the message key
        	// for the error message.  The key corresponds
        	// to a message in /src/main/resources/messages.properties
        	e.rejectValue("name", "name.empty");
        }
    }
}
